<?php
class AccpHelper extends AppHelper{
    var $path="";
    var $filepath="/custptest_ninewiz/pg/custp";
    var $servername = "";
    var $port = "";
    function AccpHelper(){
        $this->servername = "localhost";
        $this->port = 80;
        $this->path="http://www.anchora.cn:82".$this->filepath;
    }
    //用户登录
    function accplogin($data)
    {
        $url=$this->path."/login";
        $xml =$this->docurl($data,$url);
        $user=array();
        if($xml){
            foreach ($xml as $key=>$x){
                $user[$key]=$this->XmlSafeStr($x."");
            }
        }
       
        return $user;
    }
    //用户注册
    function accpreg($data){
        $url=$this->path."/reg";
        $xml=$this->docurl($data,$url);
        return $xml->guid;
    }
    //用户上传头像
    function accpiconupload($data,$type){
         App::import('Helper', 'Action');
        $actionf = new ActionHelper();
        $actionf->checkfiletype($type);
        $url=$this->path."/iconupload";
        $xml=$this->docurl($data,$url);
        return $xml->isflag;
    }
    //查看用户详细
    function accpactionuser($data){
        $url=$this->path."/actionuser";
        $xml =$this->docurl($data,$url);
        $user=array();
        if($xml){
            foreach ($xml as $key=>$x){
                $user[$key]=$this->XmlSafeStr($x."");
            }
        }
        return $user;
    }
    //用户提醒
    function accpalert($data){
//        $carrier_isNeedToAlert = false;
//        $dormitory_isNeedToAlert = false;
//        $is_alert = 0;
//        $company_funds_alert = false;
//        $url=$this->filepath."/alert";
//        if(!empty($_SESSION['User']))
//            $objuser=$_SESSION['User'];
//        $flag=true;
//        if(!empty($objuser)){
//            $fromUid=$objuser["id"];
//            if($objuser['usertype']=="1"){
//                $flag=false;
//            }
//        }else{
//            $fromUid="2";
//        }
//        //$toUid=$objuser["toUid"];
//        $guid=$data["guid"];
//        $event=$data["event"];
//        if($event == "askForTurnOver" || $event == "askForDelete" || !empty($data["carrier_isNeedToAlert"]) ||  !empty($data["companyFundsAlert"]) || !empty($data["dormitory_isNeedToAlert"])){
//            $usertype=$data["usertype"];
//            if(!empty($data["carrier_isNeedToAlert"]))
//                $carrier_isNeedToAlert = $data["carrier_isNeedToAlert"];
//            if(!empty($data["dormitory_isNeedToAlert"]))
//                $dormitory_isNeedToAlert = $data["dormitory_isNeedToAlert"];
//            if(!empty($data["is_alert"]))
//                $is_alert = $data["is_alert"];
//            if(!empty($data["companyFundsAlert"]))
//                $company_funds_alert = $data["companyFundsAlert"];
//            $par="?fromUid=".$fromUid."&guid=".base64_encode($guid)."&event=$event&usertype=$usertype&carrier_isNeedToAlert=$carrier_isNeedToAlert&dormitory_isNeedToAlert=$dormitory_isNeedToAlert&is_alert=$is_alert&companyFundsAlert=$company_funds_alert";
//            if($flag){
//                $this->ybsend($url.$par);
//            }
//        }
    }
    //异步提交数据
    function ybsend($url){
        $fp = fsockopen($this->servername, $this->port, $errno, $errstr, 30);
        if (!$fp) {
            echo "$errstr ($errno)<br />\n";exit();
        } else {
            $out = "GET $url / HTTP/1.1\r\n";
            $out .= "Host: ".$this->servername."\r\n";
            $out .= "Connection: Close\r\n\r\n";
            fwrite($fp, $out);
            /*忽略执行结果
            while (!feof($fp)) {
            echo fgets($fp, 128);
            }*/
            fclose($fp);
        }
    }
    //获取内部服务平台部门信息
    function accpgroup($data=null){
        $url=$this->path."/group";
        $xml=$this->docurl($data,$url);
        $group=array();
        $i=0;
        if($xml){
            foreach ($xml as $xl){
                $objgroup=array();
                foreach ($xl as $key=>$x){
                    $objgroup["Group"][$key]=$this->XmlSafeStr($x."");
                }
                $group[$i]=$objgroup;$i++;
            }
        }
        $grouplist=array();
        foreach ($group as $g){
            $grouplist[$g["Group"]["guid"]]=$g["Group"]["name"];
        }
        return $grouplist;
    }
    //获取用户
    function accpuser($data){
        $url=$this->path."/user";
        $xml=$this->docurl($data,$url);
        $user=array();
        $i=0;
        if($xml){
            foreach ($xml as $xl){
                $objuser=array();
                foreach ($xl as $key=>$x){
                    $objuser["User"][$key]=$this->XmlSafeStr($x."");
                }
                $user[$i]=$objuser;$i++;
            }
        }
        return $user;
    }
    //根据用户编号获取用户名
    function getusername($objusers,$userid){
        $username="";
        if($objusers){
            foreach ($objusers as $users){
                if($users["User"]["id"]==$userid){
                    $username=$users["User"]["name"];
                    break;
                }
            }
        }
        return $username;
    }
    
    //根据email获取用户id
    function getuserid($objusers,$email)
    {
        $userid = "";
        if($objusers)
        {
            foreach($objusers as $user)
            {
                if($user["User"]["email"] == $email)
                {
                    $userid = $user["User"]["id"];
                    break;
                }
            }
        }
        return $userid;
    }
    //访问连接
    function docurl($data,$url){
        $xml = "";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_POST, 1); 
        curl_setopt($curl,CURLOPT_POSTFIELDS,$data); 
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);  
        curl_setopt($curl, CURLOPT_HEADER, 0);   
        $result  = curl_exec($curl); 
        $xml = simplexml_load_string($result);
        curl_close($curl);
        return $xml;
    }
    //解码
    function XmlSafeStr($str_title){
        return empty($str_title)?"":rawurldecode($str_title);
    }
}
?>